#include "Test.h"
#include <QRandomGenerator>
#include <QDebug>

Test::Test(QWidget* parent)
    : QMainWindow(parent)
{
    qDebug() << "测试打印输出位置.......";

    initEcharts();

    m_timer = new QTimer;
    connect(m_timer, &QTimer::timeout, this,
            [&]()
    {
        static int index = 0;
        json["x"] = index++;
        //生成随机数
        json["y"] = QRandomGenerator::global()->generateDouble() * 10;
        webobj->setProperty("jsonData", json);
    });
    m_timer->start(1000);
}

Test::~Test()
{
}

void Test::initEcharts()
{
    view = new QWebEngineView(this);
    setCentralWidget(view);
    profile = view->page()->profile();

    webSetting = QWebEngineProfile::defaultProfile()->settings();
    webSetting->setAttribute(QWebEngineSettings::JavascriptEnabled, true);
    webSetting->setAttribute(QWebEngineSettings::PluginsEnabled, true);
    webSetting->setAttribute(QWebEngineSettings::JavascriptCanOpenWindows, true);

    channel = new QWebChannel(this);
    webobj = new WebClass(nullptr);

    channel->registerObject("webobj", webobj);
    view->page()->setWebChannel(channel);

    view->page()->setBackgroundColor(Qt::transparent);
    view->setContextMenuPolicy(Qt::NoContextMenu);
    QUrl url("D:/code/VScode_Qt/Demo_Msvc/src/line-stack.html");//QUrl("D:/code/BMSCharge/echart/line-stack.html")
    view->load(url);
}